package com.sunwayworld.dev.core.valueobject;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


import com.sunwayworld.dev.core.db.DBEngine;
import com.sunwayworld.dev.core.db.PrimaryKeyModel;
import com.sunwayworld.dev.core.db.TableModel;
import com.sunwayworld.dev.util.DevConfigUtil;
import com.sunwayworld.dev.util.StringDevUtil;

/**
 * 
 * @author sagitta
 * 
 */
public class JavaObject {

	private String baseSrc;

	private String srcPath;// 文件路径

	private TableModel tableModel;// 表的描述

	private Map context = new HashMap();

	// /////////////////////////////////////////public 都是要替换的内容
	public String classFullName = "";// 全路径

	public String packageName = "";// 包名

	public String vmPath = "";// 文件路径

	public String tableName = "";// 表名

	public ArrayList packages = new ArrayList();// import的包

	public String classNick = "";// 类的别名

	public String className = "";// 类名

	public String description = "";// 类描述

	public String title = "";// 类title

	public Field[] fields = null;// java 类的属性

	public Field pkField = null;// 主键 暂时不支持 主键类和多住键

	// ///////////////////////////////////////////////////
	public JavaObject(String classFullName) {
		this.classFullName = StringDevUtil.toString(classFullName);
		this.baseSrc = DevConfigUtil.getProperty("src");
		this.classNick = StringDevUtil.convertTableName(tableName);
		init();
	}

	public JavaObject(String classFullName, String vmPath) {
		this.classFullName = StringDevUtil.toString(classFullName);
		this.baseSrc = DevConfigUtil.getProperty("src");
		this.vmPath = vmPath;
		this.tableName = StringDevUtil.toUpperCase(tableName);
		this.classNick = StringDevUtil.convertTableName(tableName);
		init();

	}

	public JavaObject(String classFullName, String vmPath, String tableName)
			throws SQLException {
		this.classFullName = StringDevUtil.toString(classFullName);
		this.baseSrc = DevConfigUtil.getProperty("src");
		this.vmPath = vmPath;
		this.tableName = StringDevUtil.toUpperCase(tableName);
		this.classNick = StringDevUtil.convertTableName(tableName);

		init();
		getfieldsFromDB();
		getPackages();
	}

	@SuppressWarnings("unchecked")
	private void init() {

		String classFullName = this.classFullName;

		this.className = classFullName.substring(
				classFullName.lastIndexOf(".") + 1, classFullName.length());
		this.packageName = classFullName.substring(0, classFullName
				.lastIndexOf("." + this.className));
		this.srcPath = StringDevUtil.getSrcFilePath(baseSrc, classFullName,
				"java");

		packages = new ArrayList();
	}

	private void getfieldsFromDB() throws SQLException {// 从数据库读取 filed的信息
		if (null == tableName || "".equals(tableName)) {
			return;
		}
		DBEngine dbe = new DBEngine(tableName);
		dbe.openConnection();
		fields = dbe.getFieldsFromDB();

		tableModel = dbe.generateTableModel();
		PrimaryKeyModel[] pks = tableModel.getPkModel();

		if (null != pks && pks.length > 0) {
			String pk_colum = pk_colum = pks[0].getColumn_name();
			for (int i = 0, n = fields.length; i < n; i++) {

				if (pk_colum.equalsIgnoreCase(fields[i].getName())) {
					pkField = fields[i];

					break;
				}
			}

		}
		dbe.closeConnection();
	}

	@SuppressWarnings("unchecked")
	private Collection getPackages() {// 从返回的 VFieldInfos 中分析需要import 的package
		if (fields == null) {
			return null;
		}

		HashMap map = new HashMap();
		for (int i = 0, n = fields.length; i < n; i++) {
			String packageStr = fields[i].getType();
			if (packageStr.indexOf(".") != -1) {
				String tmp = packageStr.substring(
						packageStr.lastIndexOf(".") + 1, packageStr.length());
				fields[i].setType(tmp);
				map.put(tmp, packageStr);
			}

		}

		Collection t_packages = map.values();
		packages.addAll(t_packages);

		return packages;

	}



	/**
	 * 设置 关键字之外的 context 扩展用
	 * 
	 * @param key
	 * @param javaObject
	 */
	@SuppressWarnings("unchecked")
	public void putContext(String key, JavaObject javaObject) {

		context.put(key, javaObject);
		packages.add(javaObject.classFullName);

	}

	/**
	 * 设置 关键字之外的 context 扩展用
	 * 
	 * @param key
	 * @param javaObject[]
	 */
	@SuppressWarnings("unchecked")
	public void putContext(String key, JavaObject[] javaObjects) {

		context.put(key, javaObjects);
		if (null != javaObjects) {
			for (int i = 0, n = javaObjects.length; i < n; i++) {
				packages.add(javaObjects[i].classFullName);
			}
		}
	}

	public String getSrcPath() {
		return srcPath;
	}

	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}

	public TableModel getTableModel() {
		return tableModel;
	}

	public void setTableModel(TableModel tableModel) {
		this.tableModel = tableModel;
	}

	public String getVmPath() {
		return vmPath;
	}

	public String getBaseSrc() {
		return baseSrc;
	}

	public void setBaseSrc(String baseSrc) {
		this.baseSrc = baseSrc;
	}

	public Field[] getfields() {
		return fields;
	}

	public void setfields(Field[] fields) {
		this.fields = fields;
	}

	public String getClassFullName() {
		return classFullName;
	}

	public void setClassFullName(String classFullName) {
		this.classFullName = classFullName;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassNick() {
		return classNick;
	}

	public void setClassNick(String classNick) {
		this.classNick = classNick;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setPackages(ArrayList packages) {
		this.packages = packages;
	}

	public void setVmPath(String vmPath) {
		this.vmPath = vmPath;
	}

	public Field getPkField() {
		return pkField;
	}

	public void setPkField(Field pkField) {
		this.pkField = pkField;
	}

	public Map getContext() {
		return context;
	}

	public void setContext(Map context) {
		this.context = context;
	}

}
